import uuid

from django.db import models
from django.utils import timezone

FACTORY = [
    ('F1', 'F1'),
    ('F2', 'F2'),
]


# 产品
class Product(models.Model):
    name = models.CharField(max_length=100)
    is_valid = models.BooleanField(default=True)


# 模具改善
class MoldLog(models.Model):
    factory = models.CharField(choices=FACTORY, max_length=20)  # 0工厂
    product = models.CharField(max_length=100)  # 1 品名
    mold_no = models.CharField(max_length=100)  # 2 模具号
    problem = models.CharField(max_length=500)  # 3 生产问题
    solution = models.CharField(max_length=1000)  # 4 改善内容
    stage = models.CharField(max_length=100)  # 5 改善区间
    before = models.CharField(max_length=500, blank=True)  # 6 改善前
    after = models.CharField(max_length=500, blank=True)  # 7 改善后
    is_ok = models.CharField(max_length=20)  # 8 结果判定
    comment = models.CharField(max_length=500, blank=True)  # 9 备注
    finished = models.BooleanField(default=False, blank=True)  # 已经结束
    subject = models.CharField(max_length=100)  # 10 标题
    created = models.DateTimeField(default=timezone.now)  # 默认创建时间


# 模具改善报告文件
class FileMoldLog(models.Model):
    title = models.CharField(max_length=100)  # 原始文件名
    filename_saved = models.CharField(max_length=200)  # 保存的文件名： title+uuid
    note = models.TextField(max_length=500, blank=True)  # 备注
    version = models.PositiveIntegerField()  # 版本
    md5 = models.CharField(max_length=100, blank=True)
    mold_log = models.ForeignKey(MoldLog, on_delete=models.CASCADE)
    created = models.DateTimeField(default=timezone.now)

    def make_filename(self):
        return uuid.uuid5(uuid.NAMESPACE_DNS, self.title)
